import os

def fileInformation(file_path):
    # 处理
    if isinstance(file_path, list):  # 后期可以处理多文件
        file_path = file_path[0]
    # D:/dataset/1.txt
    metaDataList = []  # 存储数据列表
    # 获得文件的表层信息
    metaDataList.append(["File Name: ", os.path.basename(file_path)])
    # Directory Path
    metaDataList.append(["Directory Path: ", os.path.dirname(file_path)])
    # File Size
    metaDataList.append(["File Size: ", get_file_size(file_path)])

    fileHandler = open(file_path, "r")
    head_contain = ['operator', 'tester_type', 'program_path', 'handler_type', 'handler_overdrive', 'test_temperature',
                    'device_name', 'lot_id', 'wafer_id', 'test_stage', 'test_mode']
    head_flag = True
    while True:
        # Get next line from file
        line = fileHandler.readline()
        # If line is empty then end of file reached
        if not line:
            break
        lineData = line.strip()
        if head_flag:
            for head_line in head_contain:
                if head_line in lineData[: len(head_line)]:
                    if head_contain[-1] in lineData:
                        head_flag = False
                    metaDataList.append([head_line + ":", lineData.split(":")[-1]])
    # Close Close
    fileHandler.close()


    # Yield

    # Duts Tested    DUT是指芯片测试中被测芯片上的测试单元，也就是设计样品（Device Under Test）的缩写

    # Duts Passed

    # Duts Failed

    # Duts Superseded

    # Duts Unknown

    # Byte Order

    # Setup Time

    # Start Time

    # Finish Time

    # Station Number

    # Test Mode Code

    # Retest Code

    # Lot ID

    # Product ID

    # Node Name

    # Tester Type

    # Job Name

    # Job Revision

    #  Sublot ID

    # Tester Software Type

    # Tester Software Version

    # Family ID

    # File Modification

    # Handler Type (Group 2)

    # CDASystem Version
    metaDataList.append(["CDASystem Version: ", "V1.0.0"])

    print(metaDataList)

    # 格式化列表
    for i in range(len(metaDataList)):
        metaDataList[i][0] = metaDataList[i][0] + '\t'
    return metaDataList


def get_edge_file(file_path):
    """文件的表层信息读取"""
    edge_file_list = []
    # File Name:
    edge_file_list.append(["File Name: ", os.path.basename(file_path)])
    # Directory Path
    edge_file_list.append(["Directory Path: ", os.path.dirname(file_path)])
    # File Size
    edge_file_list.append(["File Size: ", get_file_size(file_path)])
    return edge_file_list

def get_file_size(p: str) -> str:
    """获得文件大小"""
    try:
        return "%.2f MB" % (os.stat(p).st_size / 2 ** 20)
    except Exception:
        return "?? MB"
    except SystemExit:
        pass


def loadInternalData(file_path):
    head_contain_list = []
    """加载内部信息"""
    fileHandler = open(file_path, "r")
    head_contain = ['operator', 'tester_type', 'program_path', 'handler_type', 'handler_overdrive', 'test_temperature', 'device_name', 'lot_id', 'wafer_id', 'test_stage', 'test_mode']
    head_flag = True
    while True:
        # Get next line from file
        line = fileHandler.readline()
        # If line is empty then end of file reached
        if not line:
            break
        lineData = line.strip()
        if head_flag:  # 当没有读取到最后的时候就继续读取
            for head_line in head_contain:
                if head_line in lineData:
                    if head_line == head_contain[-1]:   # 当最后一个列表捕获到信息就停止
                        head_flag = False
                    head_contain_list.append([head_line, lineData.split(":")[-1]])
    # Close Close
    fileHandler.close()

    return head_contain_list

if __name__ == '__main__':
    fileInformation(r"D:\dataset\rram_row3_20240605-103012_datalog_20240605103710.txt")
